// ref一般用于处理基本类型
// 如果传入复杂类型则交给reactive处理

import { hasChanged, isObject } from "../utils/utility"
import { track, trigger } from "./effect"
import { reactive } from "./reactive"

export function ref(value) {
    if (isRef(value)) {
        return value
    }
    return new RefImpl(value)
}

export function isRef(value) {
    return !!(value && value.__isRef)
}

class RefImpl {
    constructor(value) {
        this.__isRef = true
        this._value = convert(value)
    }

    get value() {
        track(this, "value")
        return this._value
    }

    set value(newValue) {
        if (hasChanged(newValue, this._value)) {
            this._value = convert(newValue)
            trigger(this, "value")
        }
    }
}

// 判断如果传入复杂类型，则转为reactive
function convert(value) {
    return isObject(value) ? reactive(value) : value
}